﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace VIFON.DAL
{
    public class DACustomer
    {
        /// <summary>
        /// Get list customer
        /// </summary>
        /// <returns></returns>
        internal List<Customer> GetCustomers(string keyWord)
        {
            try
            {
                var db = new DBVIFONDataContext();
                {
                    IQueryable<Customer> temp = db.Customers;
                    if (!string.IsNullOrEmpty(keyWord))
                        temp = temp.Where(o => o.CustomerName.IndexOf(keyWord) != -1);
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }

        internal List<Customer> GetCustomersList()
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Customers;
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Insert new Customer
        /// </summary>
        /// <param name="boCustomer"></param>
        /// <returns></returns>
        internal bool Insert(Customer boCustomer)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    db.Customers.InsertOnSubmit(boCustomer);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Update Customer
        /// </summary>
        /// <param name="boCustomer"></param>
        /// <returns></returns>
        internal bool Update(Customer boCustomer)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Customers.FirstOrDefault(o => o.Id == boCustomer.Id);
                    if (temp == null)
                        return false;
                    temp.PaymentType = boCustomer.PaymentType;
                    temp.TaxCode = boCustomer.TaxCode;
                    temp.AccountNumber = boCustomer.AccountNumber;
                    temp.Address = boCustomer.Address;
                    temp.CustomerName = boCustomer.CustomerName;
                    temp.Region = boCustomer.Region;
                    temp.Discount = boCustomer.Discount;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// Delete Customer
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal bool Delete(int id)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Customers.FirstOrDefault(o => o.Id == id);
                    if (temp == null)
                        return false;
                    db.Customers.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        internal Customer GetCustomerbyId(int id)
        {
            try
            {
                var db = new DBVIFONDataContext();
                {
                    Customer temp = db.Customers.Where(o => o.Id == id).First();
                    return temp;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
